package com.heima.leetcode.practice;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 191. 位1的个数
 * @date 2025/3/25 20:28
 */
public class E191 {
    public int hammingWeight1(int n) {
        int count = 0;
        while (n > 0) {
            int bit = n & 1;
            if (bit == 1) count++;
            n >>= 1;
        }
        return count;
    }

    public int hammingWeight2(int n) {
        int count = 0;
        while (n > 0) {
            count++;
            n &= (n - 1);
        }
        return count;
    }
}
